package com.ajie.slimming.ui;

import android.os.Handler;
import android.os.Message;
import android.support.v4.app.Fragment;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;
import android.widget.Toast;

import com.ajie.slimming.R;

public class BaseFragment extends Fragment
{
    
    /** 页面抬头标题 */
    protected TextView title_middle_text;
    
    /** 页面右上角 */
    protected TextView title_option_text;
    
    public void showToast(String message)
    {
        baseHandler.obtainMessage(MSG_TOAST_STRING, 0, 0, message)
                .sendToTarget();
    }
    
    /** Toast显示String */
    public final int MSG_TOAST_STRING = 90001;
    
    /** Toast显示int */
    public final int MSG_TOAST_INT = 90002;
    
    final Handler baseHandler = new Handler()
    {
        @Override
        public void handleMessage(Message msg)
        {
            if (getActivity() == null)
            {
                return;
            }
            switch (msg.what)
            {
                case MSG_TOAST_STRING:
                    String toastString = (String) msg.obj;
                    Toast.makeText(getActivity(),
                            toastString,
                            Toast.LENGTH_SHORT).show();
                    break;
                case MSG_TOAST_INT:
                    Toast.makeText(getActivity(), msg.arg1, Toast.LENGTH_SHORT)
                            .show();
                    break;
                default:
                    break;
            }
        }
    };
    
    public void showToast(int messageId)
    {
        // Toast.makeText(getActivity(), messageId, Toast.LENGTH_SHORT).show();
        baseHandler.obtainMessage(MSG_TOAST_INT, messageId, 0).sendToTarget();
    }
    
    protected void setTitle(int title)
    {
        title_middle_text = (TextView) getActivity().findViewById(R.id.title_text);
        title_middle_text.setText(title);
    }
    
    protected void setOption(int option)
    {
        title_option_text = (TextView) getActivity().findViewById(R.id.option_text);
        if (option != 0)
        {
            title_option_text.setVisibility(View.VISIBLE);
            title_option_text.setText(option);
        }
        else
        {
            title_option_text.setVisibility(View.GONE);
        }
    }
    
    protected void setOptionListener(OnClickListener listener)
    {
        if (null != title_option_text)
            title_option_text.setOnClickListener(listener);
    }
}
